<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <div id="app">
    <fa-cpn></fa-cpn>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

  <template id="fa-cpn">
      <div>
        <h3>我是父组件的标题</h3>
        <p>我是父组件的内容</p>
        <p>数据：{{fa_message}}</p>
        <son-cpn
          :fa_msg="fa_message"
          :fa_str="fa_str"
          :fa_arr="fa_arr"
          ref="child1"
          @sondata="getSonData"
        ></son-cpn>
        <son-cpn1 ref="child2"></son-cpn1>
        <button @click="btnClick">点击访问子组件children</button>
        <button @click="btnClick1">点击访问子组件refs</button>
      </div>
    </template>

  <script type="text/x-template" id="son-cpn">
    <div>
      <h3>我是子部组件的标题</h3>
      <p>我是子组件的内容</p>
      <p>父组件的数据：{{fa_msg}} fa_str:{{fa_str}}, fa_obj:{{fa_obj}}</p>
      <button @click='btnClick'>点我传输数据到父组件</button>
    </div>
  </script>

  <template id="son-cpn1">
      <div>
        <h3>我是子部组件1的标题</h3>
        <p>我是子组件1的内容</p>
        <button @click="btnClick">点击我访问父组件$parsent</button>
      </div>
    </template>

  <script>
    Vue.component("faCpn", {
      template: `#fa-cpn`,
      components: {
        sonCpn: {
          template: `#son-cpn`,
          props: {
            fa_msg: String,
            fa_number: [String, Number],
            fa_str: {
              type: String,
              required: true,
            },
            fa_age: {
              type: Number,
              default: 20,
            },
            fa_gender: {
              type: Number,
              enum: [0, 1, -1],
              default: -1,
            },
            fa_obj: {
              type: Object,
              // 对象或者数组必须通过工厂函数实现
              // default: function() {
              //   return {
              //     message: "hello",
              //   };
              // },
              default () {
                return {
                  message: "hello",
                };
              },
              // default: {
              //   message: "hello"
              // },
            },
            fa_arr: {
              validator(value) {
                return [1, 2, 3, 4].indexOf(value) !== -1;
              }, //不返回true报错
            },
          },
          data() {
            return {
              son_message: "我是子组件的数据",
            };
          },
          methods: {
            btnClick() {
              this.$emit("sondata", this.son_message);
            },
          },
        },
        "son-cpn1": {
          template: "#son-cpn1",
          data() {
            return {};
          },
          methods: {
            btnClick() {
              console.log(this.$parent.fa_message);
            },
          },
        },
      },
      data() {
        return {
          fa_message: "我是父组件的数据",
          fa_number: 20,
          fa_str: "hello father",
          fa_gender: 1,
          fa_arr: 1,
        };
      },
      methods: {
        getSonData(data) {
          console.log(data);
        },
        btnClick() {
          console.log(this.$children[0].son_message);
        },
        btnClick1() {
          console.log(this.$refs);
        },
      },
    });

    const app = new Vue({
      el: "#app",
      data: {
        message: "hello",
      },
    });
  </script>
</body>

</html>